package View;

import Controller.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Panel for the configuration screen. Part of a card layout on ConfigFrame
 */
public class ConfigPanel extends JPanel 
{
    JButton createButton;
    JLabel background;


    JTextField snowHeightField;
    JTextField mountainHeightField;
    JTextField plainsHeightField;
    JTextField oceanHeightField;
    JTextField minHeightField;

    JToggleButton lowPlains, medPlains, highPlains,
            lowDesert,medDesert,highDesert,
            lowForest,medForest,highForest,
            lowRiver,medRiver,highRiver,
            lowMountain,medMountain,highMountain;

    ButtonGroup plainsGroup, desertGroup, forestGroup, riverGroup, mountainGroup;
    private int plainsAmount,desertAmount,forestAmount,riverAmount,mountainAmount;

	/**
	 * Create the panel.
	 */
	public ConfigPanel()
	{
        plainsGroup = new ButtonGroup();
        desertGroup = new ButtonGroup();
        forestGroup = new ButtonGroup();
        riverGroup = new ButtonGroup();
        mountainGroup = new ButtonGroup();

        lowPlains = new JToggleButton();
        lowPlains.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/low.png")));
        lowPlains.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/lowSelected.png")));
        lowPlains.setBounds(813, 233, 73, 40);
        plainsGroup.add(lowPlains);
        this.add(lowPlains);

        medPlains = new JToggleButton();
        medPlains.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/med.png")));
        medPlains.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/medSelected.png")));
        medPlains.setBounds(892, 233, 73, 40);
        plainsGroup.add(medPlains);
        this.add(medPlains);

        highPlains = new JToggleButton();
        highPlains.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/high.png")));
        highPlains.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/highSelected.png")));
        highPlains.setBounds(972, 233, 73, 40);
        plainsGroup.add(highPlains);
        this.add(highPlains);


        lowDesert = new JToggleButton();
        lowDesert.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/low.png")));
        lowDesert.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/lowSelected.png")));
        lowDesert.setBounds(813, 312, 73, 40);
        desertGroup.add(lowDesert);
        this.add(lowDesert);

        medDesert = new JToggleButton();
        medDesert.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/med.png")));
        medDesert.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/medSelected.png")));
        medDesert.setBounds(892, 312, 73, 40);
        desertGroup.add(medDesert);
        this.add(medDesert);

        highDesert = new JToggleButton();
        highDesert.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/high.png")));
        highDesert.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/highSelected.png")));
        highDesert.setBounds(972, 312, 73, 40);
        desertGroup.add(highDesert);
        this.add(highDesert);


        lowForest = new JToggleButton();
        lowForest.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/low.png")));
        lowForest.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/lowSelected.png")));
        lowForest.setBounds(813, 393, 73, 40);
        forestGroup.add(lowForest);
        this.add(lowForest);

        medForest = new JToggleButton();
        medForest.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/med.png")));
        medForest.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/medSelected.png")));
        medForest.setBounds(892, 393, 73, 40);
        forestGroup.add(medForest);
        this.add(medForest);

        highForest = new JToggleButton();
        highForest.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/high.png")));
        highForest.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/highSelected.png")));
        highForest.setBounds(972, 393, 73, 40);
        forestGroup.add(highForest);
        this.add(highForest);


        lowRiver = new JToggleButton();
        lowRiver.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/low.png")));
        lowRiver.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/lowSelected.png")));
        lowRiver.setBounds(813, 466, 73, 40);
        riverGroup.add(lowRiver);
        this.add(lowRiver);

        medRiver = new JToggleButton();
        medRiver.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/med.png")));
        medRiver.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/medSelected.png")));
        medRiver.setBounds(892, 466, 73, 40);
        riverGroup.add(medRiver);
        this.add(medRiver);

        highRiver = new JToggleButton();
        highRiver.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/high.png")));
        highRiver.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/highSelected.png")));
        highRiver.setBounds(972, 466, 73, 40);
        riverGroup.add(highRiver);
        this.add(highRiver);


        lowMountain = new JToggleButton();
        lowMountain.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/low.png")));
        lowMountain.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/lowSelected.png")));
        lowMountain.setBounds(813, 544, 73, 40);
        mountainGroup.add(lowMountain);
        this.add(lowMountain);

        medMountain = new JToggleButton();
        medMountain.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/med.png")));
        medMountain.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/medSelected.png")));
        medMountain.setBounds(892, 544, 73, 40);
        mountainGroup.add(medMountain);
        this.add(medMountain);

        highMountain = new JToggleButton();
        highMountain.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/high.png")));
        highMountain.setSelectedIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/highSelected.png")));
        highMountain.setBounds(972, 544, 73, 40);
        mountainGroup.add(highMountain);
        this.add(highMountain);

        snowHeightField = new JTextField();
        snowHeightField.setBackground(new Color(0, 63, 94));
        snowHeightField.setForeground(Color.WHITE);
        snowHeightField.setFont(new Font("Impact", Font.PLAIN, 30));
        snowHeightField.setBounds(272, 264, 306, 40);
        snowHeightField.setColumns(4);
        this.add(snowHeightField);

        mountainHeightField = new JTextField();
        mountainHeightField.setBackground(new Color(0, 63, 94));
        mountainHeightField.setForeground(Color.WHITE);
        mountainHeightField.setFont(new Font("Impact", Font.PLAIN, 30));
        mountainHeightField.setBounds(272, 334, 306, 40);
        mountainHeightField.setColumns(4);
        this.add(mountainHeightField);

        plainsHeightField = new JTextField();
        plainsHeightField.setBackground(new Color(0, 63, 94));
        plainsHeightField.setForeground(Color.WHITE);
        plainsHeightField.setFont(new Font("Impact", Font.PLAIN, 30));
        plainsHeightField.setBounds(272, 405, 306, 40);
        plainsHeightField.setColumns(4);
        this.add(plainsHeightField);

        oceanHeightField = new JTextField();
        oceanHeightField.setBackground(new Color(0, 63, 94));
        oceanHeightField.setForeground(Color.WHITE);
        oceanHeightField.setFont(new Font("Impact", Font.PLAIN, 30));
        oceanHeightField.setBounds(272, 476, 306, 40);
        oceanHeightField.setColumns(4);
        this.add(oceanHeightField);

        minHeightField = new JTextField();
        minHeightField.setBackground(new Color(0, 63, 94));
        minHeightField.setForeground(Color.WHITE);
        minHeightField.setFont(new Font("Impact", Font.PLAIN, 30));
        minHeightField.setBounds(272, 547, 306, 40);
        minHeightField.setColumns(4);
        this.add(minHeightField);

        createButton = new JButton("");
        createButton.setIcon(new ImageIcon(ConfigPanel.class.getResource("/Sprites/generateButton.png")));
        createButton.setBounds(1007, 618, 250, 80);
        createButton.addMouseListener(addButtonLogic());
        this.add(createButton);

        background = new JLabel("");
        background.setBounds(0, 0, 1280, 720);
        this.add(background);
        background.setIcon(new ImageIcon(ConfigFrame.class.getResource("/Sprites/mapgenconfigscreen.png")));
        this.setDefault();
	}

    public void setDefault()
    {
         snowHeightField.setText("300");
         mountainHeightField.setText("280");
         plainsHeightField.setText("180");
         oceanHeightField.setText("50");
         minHeightField.setText("0");

         medPlains.setSelected(true);
         medDesert.setSelected(true);
         medForest.setSelected(true);
         medRiver.setSelected(true);
         medMountain.setSelected(true);
    }

    /**
     * Creates the logic for a mouse adapter for a button that, when clicked, will send the info from the config screen
     * to the map generator.
     * @return
     */
    public MouseAdapter addButtonLogic()
    {
        MouseAdapter m = new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                Main.setUpMapGenerator(getSnowHeight(), getMountainHeight(), getPlainsHeight(), getOceanHeight(), getMinHeight(), getPlainsAmount(), getDesertAmount(), getForestAmount(), getRiverAmount(), getMountainAmount());
            }
        };
        return m;
    }
        //Getters for the Config Screen components

    public int getPlainsAmount()
    {
        if(lowPlains.isSelected())
        {
            return 0;
        }
        else if(highPlains.isSelected())
        {
            return 2;
        }
        return 1;
    }

    public int getDesertAmount()
    {
        if(lowDesert.isSelected())
        {
            return 0;
        }
        else if(highDesert.isSelected())
        {
            return 2;
        }
        return 1;
    }
    public int getForestAmount()
    {
        if(lowForest.isSelected())
        {
            return 0;
        }
        else if(highForest.isSelected())
        {
            return 2;
        }
        return 1;
    }
    public int getRiverAmount()
    {
        if(lowRiver.isSelected())
        {
            return 0;
        }
        else if(highRiver.isSelected())
        {
            return 2;
        }
        return 1;
    }
    public int getMountainAmount()
    {
        if(lowMountain.isSelected())
        {
            return 0;
        }
        else if(highMountain.isSelected())
        {
            return 2;
        }
        return 1;
    }

    public int getSnowHeight()
    {
        int i;
        try
        {
            i = Integer.parseInt(snowHeightField.getText());
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
        return i;
    }

    public int getMountainHeight()
    {
        int i;
        try
        {
            i = Integer.parseInt(mountainHeightField.getText());
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
        return i;
    }

    public int getPlainsHeight()
    {
        int i;
        try
        {
            i = Integer.parseInt(plainsHeightField.getText());
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
        return i;
    }

    public int getOceanHeight()
    {
        int i;
        try
        {
            i = Integer.parseInt(oceanHeightField.getText());
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
        return i;
    }

    public int getMinHeight()
    {
        int i;
        try
        {
            i = Integer.parseInt(minHeightField.getText());
        }
        catch(NumberFormatException e)
        {
            return -1;
        }
        return i;
    }
}
